<?php
declare(strict_types=1);
namespace App\Http\Controller;

use Psr\Http\Message\RequestInterface;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Validate\CheckParam;
use Tool\AjaxReturn;
use App\Model\Dao\Specific\IServerUser;
use App\Model\Dao\Specific\IFriendServer;
use Vadlidate\ValidateFriend;
use Swoft\Http\Server\Annotation\Mapping\Middleware;
use App\Http\Middleware\ValidateLoginMiddleware;

/**
 * Class FriendController
 * @Controller(prefix="friend")
 * @Middleware(ValidateLoginMiddleware::class)
 * @package App\Http\Controller
 */
class FriendController
{
    /**
     * @Inject()
     * @var IFriendServer
     */
    private IFriendServer $server;
    
    /**
     * @Inject()
     * @var IServerUser
     */
    private IServerUser $userServer;

    /**
     * @RequestMapping(name="addFriend", method={RequestMethod::POST})
     * @param RequestInterface $request
     * @return array
     */
    public function addFriend(Request $request): array
    {
        $data = $request->post();
        
        $check = new CheckParam(ValidateFriend::getAddFriendRule(), $data);
        
        if (!$check->detectionParameters()) {
            return AjaxReturn::error($check->getErrorMessage());
        }
        
        $status = $this->server->addFriendStatus($data);
       
        if (false === $status) {
            return AjaxReturn::error('已添加用户或其他错误');
        }
        
        return AjaxReturn::sendData(1);
    }
    
    /**
     * @RequestMapping(name="getFriends", method={RequestMethod::POST})
     * @param Request $request
     * @return array
     */
    public function getFriends(Request $request): array
    {
        $friendCollection = $this->server->getFriendIds($request->post());
        
        if (0 === $friendCollection->count()) {
            return AjaxReturn::sendData([]);
        }
        
        return AjaxReturn::sendData($this->userServer->getUserDataByFriendId($friendCollection));
    }
    
}